package leetcode;

import util.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project : algorithm-learning-java
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/1/26 16:49
 */
public class Q0199 {
    List<Integer> res = new ArrayList<>();

    private void dfs(TreeNode node, int k) {
        if (node == null) return;
        if (k > res.size()) res.add(0);
        res.set(k - 1, node.val);
        dfs(node.left, k + 1);
        dfs(node.right, k + 1);
    }

    public List<Integer> rightSideView(TreeNode root) {
        dfs(root, 1);
        return res;
    }
}
